﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using Telerik.Web.UI;

namespace Medianamik.UI.UserControls
{
    public partial class MainMenu : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void menuTest_ItemDataBound(object sender, RadMenuEventArgs e)
        {
            //On doit travailler avec le level étant donné que le SiteMap contient différents types d'objets

            var node = (SiteMapNode)e.Item.DataItem;

            switch (e.Item.Level)
            {
                case 0:
                    e.Item.Visible = (node.HasChildNodes && node.ChildNodes.Cast<SiteMapNode>()
                        .Where(n => bool.Parse(n["DisplayInMenu"])).Count() > 0);
                    e.Item.Text = "";
                    e.Item.ExpandedImageUrl = node["SelectedImageUrl"];
                    e.Item.HoveredImageUrl = node["SelectedImageUrl"];
                    e.Item.ImageUrl = IsMenuGroupSelected(node) ? node["SelectedImageUrl"] : node["ImageUrl"];
                    break;
                case 1:
                    e.Item.Visible = bool.Parse(node["DisplayInMenu"]);
                    break;
                default:
                    throw new Exception("Main menu of Medianamik should be restrained to two levels.");
            }
        }

        private bool IsMenuGroupSelected(SiteMapNode node)
        {
            return SiteMap.CurrentNode != null && SiteMap.CurrentNode.ParentNode != null &&
                   SiteMap.CurrentNode.ParentNode == node;
        }
    }
}